package com.example.human_resource_management.mapper;

import com.example.human_resource_management.entry.dto.ResourceApplyDto;
import com.example.human_resource_management.entry.po.ResourceApply;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【resource_apply(资源申请表)】的数据库操作Mapper
* @createDate 2025-06-09 16:40:16
* @Entity com.example.human_resource_management.entry.po.ResourceApply
*/
public interface ResourceApplyMapper extends BaseMapper<ResourceApply> {

    @Select("""
        SELECT 
            ra.apply_id, ra.emp_id, 
            r.res_name, r.res_type, r.description,
            d.dept_name,
            ra.apply_time, ra.approval_status
        FROM 
            resource_apply ra
        LEFT JOIN 
            resource r ON ra.res_id = r.res_id
        LEFT JOIN 
            department d ON r.dept_id = d.dept_id
        WHERE 
            ra.emp_id = #{empId}
    """)
    List<ResourceApplyDto> selectResourceAppListByEmpId(Long empId);

    @Update("""
        UPDATE Resource_Apply 
        SET approval_status = #{newStatus}
        WHERE apply_id = #{applyId} 
          AND emp_id = #{empId} 
          AND approval_status = '待审'
        """)
    int cancelApply(@Param("applyId") Long applyId, @Param("empId") Long empId, @Param("newStatus") String newStatus);

    //管理员端
    @Select("""
        SELECT 
            ra.apply_id, ra.emp_id, 
            r.res_name, r.res_type, r.description,
            d.dept_name,
            ra.apply_time, ra.approval_status
        FROM 
            resource_apply ra
        LEFT JOIN 
            resource r ON ra.res_id = r.res_id
        LEFT JOIN 
            department d ON r.dept_id = d.dept_id
    """)
    List<ResourceApplyDto> selectAllResourceAppList();

    @Insert("INSERT INTO resource_apply (emp_id, res_id, apply_time, approval_status) VALUES (#{empId}, #{resId}, #{applyTime}, #{approvalStatus})")
    int insertResourceApply(ResourceApply resourceApply);

    @Update("UPDATE resource_apply SET emp_id = #{empId}, res_id = #{resId}, apply_time = #{applyTime}, approval_status = #{approvalStatus} WHERE apply_id = #{applyId}")
    int updateResourceApply(ResourceApply resourceApply);

    @Delete("DELETE FROM resource_apply WHERE apply_id = #{applyId}")
    int deleteResourceApply(Long applyId);
}




